package Table;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.Collator;
import java.util.Locale;
import java.util.Vector;

import javax.swing.JTable;

public class CustJTable extends JTable {
	
	public static void main(String[] args) {
		Vector tableContVect1 = new Vector<String>();
		tableContVect1.add("列1");
		tableContVect1.add("列2");
		tableContVect1.add("列3");
		Vector colNameVect1=new Vector<String>();
		colNameVect1.add("内容1");
		colNameVect1.add("内容2");
		colNameVect1.add("内容3");
		CustJTable t = new CustJTable(tableContVect1,colNameVect1,300,400,new int[]{50,50,50});
	}
	
	Vector tableContVect;
	Vector colNameVect;
	int length;
	int width;
	boolean[] flag;
	CustJTable table;
	int[] columnWidths;

	public CustJTable(Vector tableContVect, Vector colNameVect, int length, int width, int[] columnWidths) {
		this.tableContVect = tableContVect;
		this.colNameVect = colNameVect;
		this.length = length;
		this.width = width;
		this.columnWidths = columnWidths;
		this.table = this;
		initialize();
	}

	public Vector getTableContVect() {
		return tableContVect;
	}

	public void setTableContVect(Vector tableContVect) {
		this.tableContVect = tableContVect;
	}
	CustTableModel model =null;
	public CustTableModel getModel() {
		return model;
	}

	private void initialize() {
		model = new CustTableModel(this.tableContVect, this.colNameVect);
		setModel(model);
		setPreferredScrollableViewportSize(new Dimension(this.length, this.width));
		setAutoResizeMode(0);
		this.flag = new boolean[this.colNameVect.size()];
		for (int i = 0; i < this.colNameVect.size(); ++i) {
			this.flag[i] = false;
		}
		for (int i = 0; i < getColumnCount(); ++i)
			getColumnModel().getColumn(i).setPreferredWidth(columnWidths[i]);
		getTableHeader().addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseevent) {
				int i = columnAtPoint(mouseevent.getPoint());
				int j = convertColumnIndexToModel(i);
				flag[j] = !flag[j];
				Collator cnCollator = Collator.getInstance(Locale.getDefault());
				if (flag[j]) {
					for (int k = 0; k < tableContVect.size(); ++k) {
						for (int l = 0; l < tableContVect.size() - k - 1; ++l) {
							Object[] o1 = (Object[]) tableContVect.elementAt(l);
							Object[] o2 = (Object[]) tableContVect.elementAt(l + 1);
							if (cnCollator.compare(o1[j].toString(), o2[j].toString()) >= 0)
								continue;
							Object[] o3 = o1;
							o1 = o2;
							o2 = o3;
							tableContVect.set(l, o1);
							tableContVect.set(l + 1, o2);
						}

					}

				} else {
					for (int k = 0; k < tableContVect.size(); ++k) {
						for (int l = 0; l < tableContVect.size() - k - 1; ++l) {
							Object[] o1 = (Object[]) tableContVect.elementAt(l);
							Object[] o2 = (Object[]) tableContVect.elementAt(l + 1);
							if (cnCollator.compare(o1[j].toString(), o2[j].toString()) <= 0)
								continue;
							Object[] o3 = o1;
							o1 = o2;
							o2 = o3;
							tableContVect.set(l, o1);
							tableContVect.set(l + 1, o2);
						}
					}
				}

				table.repaint();
			}
		});
		
		
		// this.table.getModel().addTableModelListener(new TableModelListener()
				// {
				// public void tableChanged(TableModelEvent e) {
				// System.out.println("按回车键改变");
				// }
				// });
				
				
//				table.setCellSelectionEnabled(true);// 使得表格的选取是以cell为单位,而不是以列为单位.若你没有写此行,则在选取表格数据时以整列为单位.
//				ListSelectionModel selectionMode = table.getSelectionModel();// 取得table的ListSelectionModel.
//				selectionMode = table.getSelectionModel();// 取得table的ListSelectionModel.
//				selectionMode.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);// 单一选择，否则点击释放会触发两次
//				selectionMode.addListSelectionListener(new ListSelectionListener() {
//					public void valueChanged(ListSelectionEvent e) {
//						String tempString = "";
//						boolean isMouseRealed= e.getValueIsAdjusting();//判断鼠标释放
//						int[] rows = table.getSelectedRows();
//						int[] columns = table.getSelectedColumns();
//						int row = table.getSelectedRow();
//						int column = table.getSelectedColumn();
//						tempString = (String) table.getValueAt(row, column);
//						if(!isMouseRealed){
//						//System.out.println("选择的数据为" + tempString);
//						}
//					}
//				});
				
				// this.table.addMouseListener(new MouseAdapter() {
				// public void mouseClicked(MouseEvent mouseevent) {
				// try {
				// System.out.println("鼠标点击");
				// int[] rows = getSelectedRows();
				// for (int i = 0; i < rows.length; ++i) {
				// System.out.println("选中行的值："
				// + table.getValueAt(rows[i], 1));
				// String bo = (String) table.getValueAt(rows[i], 1);
				// table.setValueAt(bo, rows[i], 1);
				// }
				// table.repaint();
				// } catch (Exception e) {
				// e.printStackTrace();
				// }
				// }
				// });
		
	}
}